const swap = (arr, i, j) => {
    const temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
}

const insertSort = arr => {
    let temp = 0, j = 0, len = arr.length

    for (let i = 1; i < len; i++){
        temp = arr[i]
        j = i

        while (j > 0 && (arr[j - 1] >  temp)){
            arr[j] = arr[j - 1]
            j--
        }

       // swap(arr, i, j)// 不能这么写，第一次执行arr[j] = arr[j - 1]时，i === j, 所以i的值变成了j-1的值，并不是真正初始的arr[i]
        arr[j] = temp
    }

    return arr
}

console.log(insertSort([10, 3, 12, 1]))

